<template>
  <div id="DataMaintenance-studentTypeSetting">
    <div class="flex-all pt10 pb10 pl12 pr12 bg-white height50">
      <div class="ct-head-left flex-al">
        <span class="left-line-orange mr10"></span>
        <span class="color-333 fs14">幼儿叮嘱回复模板</span>
      </div>
      <div class="cr-head-right">
        <el-button type="primary" size="mini" @click="handAdd">{{
          $t("gradenSet.gradenSet.ReasonForTrial.50zgt2bmc3g0")
        }}</el-button>
      </div>
    </div>
    <div class="pAll12 bt_EBEEF5">
      <div class="tableDiv" ref="auto_height_table">
        <settingTableCont :tableData="tableList">
          <!-- 按钮操作组********start -->
          <el-table-column
            ref="fixedColumn"
            :label="$t('gradenSet.gradenSet.ReasonForTrial.50zgt2bmd1w0')"
            align="center"
            width="120px"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click.native.prevent="handEdit(scope.row)"
                >{{
                  $t("gradenSet.gradenSet.ReasonForTrial.50zgt2bmde40")
                }}</el-button
              >
              <el-button
                type="text"
                size="small"
                @click.native.prevent="handDelete(scope.row)"
                >{{
                  $t("gradenSet.gradenSet.ReasonForTrial.50zgt2bmdpg0")
                }}</el-button
              >
            </template>
          </el-table-column>
          <!-- 按钮操作组********end -->
        </settingTableCont>
        <!-- 分页 -->
        <el-pagination
          style="margin: 10px"
          :current-page.sync="paging.curPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size.sync="paging.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paging.pageTotal"
          @current-change="curPageChange"
          @size-change="pageSizeChange"
        ></el-pagination>
      </div>
    </div>
    <!-- 侧边弹窗******新增或者编辑学生取消柜询/试听原因 -->
    <el-drawer
      :title="isEdit ? '编辑回复模板' : '添加回复模板'"
      :visible.sync="cancelVisible"
      direction="rtl"
      size="500px"
      :modal="false"
      custom-class="pop-slider--box"
      :before-close="handleClose"
    >
      <div class="slider-btn">
        <el-button
          v-if="!isEdit"
          type="primary"
          size="mini"
          @click="onSubmit"
          >{{
            $t("gradenSet.gradenSet.ReasonForTrial.50zgt2bme0g0")
          }}</el-button
        >
        <el-button v-else type="primary" size="mini" @click="onEditSubmit">{{
          $t("gradenSet.gradenSet.ReasonForTrial.50zgt2bme0g0")
        }}</el-button>
      </div>
      <div class="slider-content pAll20">
        <el-form
          label-width="110px"
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="模板标题" prop="template_title">
            <el-input
              clearable
              placeholder="请输入模板标题"
              v-model="ruleForm.template_title"
              style="width: 320px"
              type="textarea"
              autosize
            ></el-input>
          </el-form-item>
          <el-form-item label="模板内容" prop="template_content">
            <el-input
              type="textarea"
              clearable
              v-model="ruleForm.template_content"
              autosize
              placeholder="请输入模板内容"
              style="width: 320px"
            ></el-input>
          </el-form-item>
        </el-form>
        <p class="color-red">
          * {{ $t("gradenSet.gradenSet.ReasonForTrial.50zgt2bmf9s0") }}
        </p>
      </div>
    </el-drawer>
    <!-- 删除取消柜询/试听原因 -->
    <popTips
      :dialogVisable="delItem"
      :popTipsCont="popTips_del"
      @handConfirm="delWarnTemplateAction"
      @handCancel="delItem = false"
      @handleCloseTip="delItem = false"
    ></popTips>
  </div>
</template>

<script>
import settingTableCont from "@/components/tables/settingTableCont.vue";
import popTips from "@/components/common/popTips/popTips.vue";
import setHttp from "../../Set";

export default {
  name: "setting",
  components: {
    settingTableCont,
    popTips,
  },
  data() {
    return {
      delItem: false, //删除学生取消柜询/试听原因
      cancelVisible: false, //新增或者编辑职务
      isEdit: false, //是否编辑职务
      template_id: "",
      //删除学生取消柜询/试听原因****弹窗
      popTips_del: {
        btn_text1: this.$t("gradenSet.gradenSet.ReasonForTrial.50zgt2bmfjw0"),
        btn_text2: this.$t("gradenSet.gradenSet.ReasonForTrial.50zgt2bmfuk0"),
        title: "删除回复模板",
        context: "是否删除此回复模板?",
      },
      //检索-table
      filter: { keyword: "" },
      // 表格数据
      tableList: {
        // 表格的列
        columns: [],
        // 表格数据
        list: [],
        options: {
          errortip: "", //空页面提示内容
          mutiSelect: false, //是否显示多选
          issetting: false, //是否可以编辑列
          tableHeight: 0,
        },
      },
      // 分页
      paging: {
        curPage: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      // 表单
      ruleForm: {
        template_title: "",
        template_content: "",
      },
      // 表单验证
      rules: {
        template_title: [
          {
            required: true,
            message: "请输入模板标题",
            trigger: "change",
          },
        ],
        template_content: [
          {
            required: true,
            message: "请输入模板内容",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getWarnTemplateList();
  },
  methods: {
    //园务相关设置 列表
    getWarnTemplateList() {
      setHttp
        .getWarnTemplateList({
          is_count: 1, //是否需要总数，可以不传
          p: this.paging.curPage,
          num: this.paging.pageSize,
          keyword: this.filter.keyword,
        })
        .then((res) => {
          this.paging.pageTotal = parseInt(res.data.result.allnum);
          this.tableList.columns = res.data.result.field;
          if (res.data.error == 0) {
            this.tableList.list = res.data.result.list;
          } else {
            this.tableList.list = [];
            this.tableList.options.errortip = res.data.errortip;
          }
        });
    },
    //检索
    search() {
      this.paging.curPage = 1;
      this.getWarnTemplateList();
    },
    //新增
    handAdd() {
      this.isEdit = false; //是否编辑
      this.cancelVisible = !this.cancelVisible;
      this.resetForm("ruleForm");
    },
    //编辑
    handEdit(v) {
      this.template_id = v.template_id;
      this.resetForm("ruleForm");
      this.ruleForm = {
        template_title: v.template_title,
        template_content: v.template_content,
      };
      this.isEdit = true;
      this.cancelVisible = !this.cancelVisible;
      // setHttp
      //   .getStuTypesOneApi({
      //     template_id: this.template_id,
      //   })
      //   .then((res) => {
      //     let data = res.data.result.list;
      //     this.ruleForm = {
      //       template_title: data.template_title,
      //       template_content: data.template_content,
      //     };
      //     this.isEdit = true;
      //     this.cancelVisible = !this.cancelVisible;
      //   });
    },
    //删除
    handDelete(v) {
      this.template_id = v.template_id;
      this.delItem = !this.delItem;
    },
    //删除取消柜询/试听原因
    delWarnTemplateAction() {
      setHttp
        .delWarnTemplateAction({
          template_id: this.template_id,
        })
        .then((res) => {
          if (res.data.error == 0) {
            this.$message({ message: res.data.errortip, type: "success" });
            this.delItem = false;
            this.getWarnTemplateList();
          } else {
            this.$message.error(res.data.errortip);
          }
        });
    },
    //侧边弹窗 - 新增 - 提交
    onSubmit() {
      let canSubmit = this.$Tool.isNull("ruleForm", this);
      if (canSubmit) {
        setHttp
          .addWarnTemplateAction({
            template_title: this.ruleForm.template_title,
            template_content: this.ruleForm.template_content,
          })
          .then((res) => {
            if (res.data.error == 0) {
              this.$message({ message: res.data.errortip, type: "success" });
              this.cancelVisible = false;
              this.getWarnTemplateList();
            } else {
              this.$message.error(res.data.errortip);
            }
          });
      }
    },
    //侧边弹窗 - 编辑 - 提交
    onEditSubmit() {
      let canSubmit = this.$Tool.isNull("ruleForm", this);
      if (canSubmit) {
        setHttp
          .updateWarnTemplateAction({
            template_id: this.template_id,
            template_title: this.ruleForm.template_title,
            template_content: this.ruleForm.template_content,
          })
          .then((res) => {
            if (res.data.error == 0) {
              this.$message({ message: res.data.errortip, type: "success" });
              this.cancelVisible = false;
              this.getWarnTemplateList();
            } else {
              this.$message.error(res.data.errortip);
            }
          });
      }
    },

    // 页码变化
    curPageChange(v) {
      this.paging.curPage = v;
      this.getWarnTemplateList();
    },
    // pageSize改变
    pageSizeChange() {
      this.paging.curPage = 1;
      this.getWarnTemplateList();
    },
    // 重置表单
    resetForm(form) {
      // 数据清空
      this.ruleForm = {
        template_title: "",
        template_content: "",
      };
      if (this.$refs[form]) {
        this.$refs[form].resetFields();
      }
    },
    //侧边弹窗-关闭
    handleClose(done) {
      done();
    },
  },
  mounted() {
    this.$nextTick(() => {
      if (this.$refs["auto_height_table"]) {
        this.tableList.options.tableHeight = this.$Tool.getTableHeight(
          this.$refs.auto_height_table.getBoundingClientRect().top + 55
        );
      }
    });
  },
};
</script>
